//
//  BaseEmptyView.swift
//  cockpit
//
//  Created by Haijun Si on 2020/7/15.
//  Copyright © 2020 chenganrt. All rights reserved.
//

import UIKit
import RxSwift
import RxOptional

class BaseEmptyView: UIView {

    let refreshBtnClick:PublishSubject = PublishSubject<Int?>()
    
    var imgName:String? {
        didSet {
            if let imgName = imgName {
                self.nullDataImg.image = UIImage.init(named: imgName)
            }
        }
    }
    
    var labelText:String? {
        didSet {
            if let labelText = labelText {
                self.nullDataLabel.text = labelText
            }
        }
    }
    
    var isShowRefreshBtn:Bool? {
        didSet {
            if isShowRefreshBtn ?? false {
                self.refreshDataBtn.isHidden = false
                
                self.refreshDataBtn.rx.tap
                .subscribe(onNext:{ [weak self] in
                    self?.refreshBtnClick.onNext(0)
                })
                    .disposed(by: rx.disposeBag)
            }
        }
    }
    
    
    private lazy var nullDataImg:UIImageView = UIImageView()
    
    private var nullDataLabel:UILabel = UILabel().then { (label) in
        label.textColor = CPColor666
        label.font = CPFont(font: 16)
        label.textAlignment = NSTextAlignment.center
        label.text = "暂无数据"
    }
    
    private lazy var refreshDataBtn:UIButton = UIButton().then { (btn) in
        btn.setTitleColor(CPColor666, for: UIControl.State.normal)
        btn.titleLabel?.font = CPFont(font: 16)
        btn.setTitle("刷新数据", for: UIControl.State.normal)
    }
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        setUIMethod()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUIMethod() {
        
        if imgName != nil {
            self.addSubview(self.nullDataImg)
            self.nullDataImg.snp_makeConstraints { (make) in
                make.left.equalTo(25)
                make.right.equalTo(-25)
                make.top.equalTo(CPNavBarHeight + 50)
                make.height.equalTo(200)
            }
        }
        
        self.addSubview(self.nullDataLabel)
        self.nullDataLabel.snp_makeConstraints { (make) in
            make.left.equalTo(25)
            make.right.equalTo(-25)
            if imgName != nil {
                make.top.equalTo(self.nullDataImg.snp_bottom).offset(20)
            }
            else {
                make.centerY.equalTo(self)
            }
            make.height.equalTo(20)
        }
        
        
        self.addSubview(self.refreshDataBtn)
        self.refreshDataBtn.isHidden = true
        self.refreshDataBtn.snp_makeConstraints { (make) in
            make.left.equalTo(25)
            make.right.equalTo(-25)
            make.top.equalTo(self.nullDataLabel.snp_bottom).offset(20)
            make.height.equalTo(40)
        }
    }


}
